//
//  CLclassifyViewController.m
//  I-want-go
//
//  Created by dllo on 15/10/26.
//  Copyright (c) 2015年 lanou3g. All rights reserved.
//

#import "CLclassifyViewController.h"
#import "CLconcreteTableCell.h"
#import "BaseModel.h"
#import "CLpic.h"
#import "CLunder.h"
#import "CLunderTableCell.h"
#import "CLtool.h"
#import "CLsmallViewController.h"
#import "CLinterViewController.h"
#import "CLscreen.h"

//#import "CLarticleCollectionCell.h"
#import "CLsmallViewController.h"

@interface CLclassifyViewController ()<UITableViewDataSource, UITableViewDelegate, CLconcreteTableCellDelegate>

@property(nonatomic, retain)UITableView *myTableVIew;
@property(nonatomic, retain)UICollectionView *clsaaifyCollection;
@property(nonatomic, retain)NSMutableArray *arr;
@property(nonatomic, retain)NSMutableArray *proArr;
@property(nonatomic, retain)NSString *sttt;
@property(nonatomic, assign)NSInteger page;
@end


@implementation CLclassifyViewController

- (void)dealloc {
    [_sttt release];
    [_arr release];
    [_proArr release];
    [_myTableVIew release];
    [_clsaaifyCollection release];
    [super dealloc];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.myTableVIew.backgroundColor = [UIColor redColor];
    self.myTableVIew = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, 375 * SWIDTH, 604 * SHEIGHT)];
    [self.view addSubview:self.myTableVIew];
    [_myTableVIew release];
    
//    self.title = self.titl;
    
    self.myTableVIew.dataSource = self;
    self.myTableVIew.delegate = self;
    
    self.myTableVIew.footer = [MJRefreshBackNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(footerAction)];
    
    [self createData];
    
    [self createDete];
}
//// 解析数据 上面的
- (void)createData {

    NSString *str = @"http://api.xiangqu.com/category/out/children";
    NSURL *url = [NSURL URLWithString:str];
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
    [request setHTTPMethod:@"POST"];
    NSString *sqq = [NSString stringWithFormat:@"id=%@&key=5b745e19039468e23cc4f9e915a1805f&t=1445909497431&userid=705811", self.strId];
    NSString *bodyStr = sqq;
    NSData *bodyData = [bodyStr dataUsingEncoding:NSUTF8StringEncoding];
    [request setHTTPBody:bodyData];
    [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
        NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
        
        self.arr = [NSMutableArray array];
        self.arr = [CLpic baseModelByArr:dic[@"data"]];
//        NSLog(@"%@", self.arr);
        
        
        [self.myTableVIew reloadData];
    }];
}

// 上拉
- (void)footerAction {
    
    self.page = 0;
    self.page++;
    NSString *str = @"http://api.xiangqu.com/search/list";
    NSURL *url = [NSURL URLWithString:str];
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
    [request setHTTPMethod:@"POST"];
    NSString *bodyStr =[NSString stringWithFormat: @"fromType=-1&imageHD=2&key=04f94d7ab62cfd07b33000cefa993cd4&outCategoryId=%@&page=%ld&t=1445914166026&userid=705811&version=3.5.4", self.strId, self.page];
    NSData *bodyData = [bodyStr dataUsingEncoding:NSUTF8StringEncoding];
    [request setHTTPBody:bodyData];
    [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
        NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
        
        NSMutableArray *arArr = [NSMutableArray array];
        arArr = [CLunder baseModelByArr:dic[@"data"][@"records"]];
        for (CLunder *under in arArr) {
            [self.proArr addObject:under];
        }
        [self.proArr removeLastObject];
        [self.myTableVIew.footer endRefreshing];
        [self.myTableVIew reloadData];
        
    }];

}


////// 下面的数据
- (void)createDete {
    NSString *str = @"http://api.xiangqu.com/search/list";
    NSURL *url = [NSURL URLWithString:str];
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
    [request setHTTPMethod:@"POST"];
    NSString *sqq = [NSString stringWithFormat:@"fromType=-1&imageHD=2&key=04f94d7ab62cfd07b33000cefa993cd4&outCategoryId=%@&page=0&t=1445914166026&userid=705811&version=3.5.4", self.strId];
    NSString *bodyStr = sqq;
    NSData *bodyData = [bodyStr dataUsingEncoding:NSUTF8StringEncoding];
    [request setHTTPBody:bodyData];
    [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
        NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
        self.proArr = [NSMutableArray array];
        self.proArr = [CLunder baseModelByArr:dic[@"data"][@"records"]];
        
        [self.myTableVIew reloadData];
    }];
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (section == 1) {
        return self.proArr.count;
    } else {
        return 1;
    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    self.sttt = [NSString stringWithFormat:@"%@", self.strId];
    
    if (indexPath.section == 0 && ![self.sttt isEqualToString:@"7"]) {
        self.myTableVIew.rowHeight = 340 * SHEIGHT;
        static NSString *reuse = @"reuse";
        CLconcreteTableCell *cell = [tableView dequeueReusableCellWithIdentifier:reuse];
        if (!cell) {
            cell = [[CLconcreteTableCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:reuse];
        }
        // 协议
        cell.Delat = self;
        
        cell.pictArr = self.arr;
        [cell.consCollectionView reloadData];
        return cell;

    } else if (indexPath.section == 0 && [self.sttt isEqualToString:@"7"]) {
        self.myTableVIew.rowHeight = 250 * SHEIGHT;
        static NSString *reuse = @"raae";
        CLconcreteTableCell *cell = [tableView dequeueReusableCellWithIdentifier:reuse];
        if (!cell) {
            cell = [[CLconcreteTableCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:reuse];
        }
        // 协议
        cell.Delat = self;
        
        cell.pictArr = self.arr;
        [cell.consCollectionView reloadData];
        return cell;
    }
         else {
        self.myTableVIew.rowHeight = 180 * SHEIGHT;
        static NSString *reuse = @"raaaa";
        CLunderTableCell *cell = [tableView dequeueReusableCellWithIdentifier:reuse];
        if (!cell) {
            cell = [[CLunderTableCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:reuse];
        }
        
        CLunder *underPro = self.proArr[indexPath.row];
        [cell.leftImage sd_setImageWithURL:[NSURL URLWithString:underPro.image]];
        cell.leftAddlabel.text = underPro.brand;
        cell.leftproducrLabel.text = underPro.productDescription;
             //    无色
             cell.selectionStyle = UITableViewCellSelectionStyleNone;
             return cell;

        return cell;
    }
    
}

- (void)pushNext:(NSString *)nextId Withtitle:(NSString *)title {
    
    CLsmallViewController *smallVC = [[CLsmallViewController alloc] init];
    [self.navigationController pushViewController:smallVC animated:YES];
    smallVC.sssId = nextId;
    smallVC.mytitle = title;
    
}



- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
        return 2;
}

- (CGFloat)tableView:(UITableView *)tableView estimatedHeightForHeaderInSection:(NSInteger)section {
    if (section == 1) {
        return 40 * SHEIGHT;
    } else {
        return 0;
    }
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width * SWIDTH, 50 * SHEIGHT)];
    headerView.backgroundColor = [UIColor whiteColor];
    
    UIButton *leftButton = [UIButton buttonWithType:UIButtonTypeSystem];
    leftButton.frame = CGRectMake(130 * SWIDTH, 0, 100 * SWIDTH, 40 * SHEIGHT);
    leftButton.backgroundColor = [UIColor whiteColor];
    [leftButton setTitle:@"精          选" forState:UIControlStateNormal];
    [leftButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    
    [headerView addSubview:leftButton];
    
    UIView *leftView = [[UIView alloc] initWithFrame:CGRectMake(50 * SWIDTH, 20 * SHEIGHT, 70 * SWIDTH, 1 * SHEIGHT)];
    leftView.backgroundColor = [UIColor blackColor];
    [headerView addSubview:leftView];
    [leftView release];
    
    UIView *rightView = [[UIView alloc] initWithFrame:CGRectMake(240 * SWIDTH, 20 * SHEIGHT, 70 * SWIDTH, 1 * SHEIGHT)];
    rightView.backgroundColor = [UIColor blackColor];
    [headerView addSubview:rightView];
    [rightView release];
    
    return headerView;
    
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    CLinterViewController *intVC = [[CLinterViewController alloc] init];
    [self.navigationController pushViewController:intVC animated:YES];
    
    CLunder *under = self.proArr[indexPath.row];
    intVC.interId = [NSString stringWithFormat:@"%@", under.productId];
    
}

- (void)viewWillAppear:(BOOL)animated {
    
    self.tabBarController.tabBar.hidden = YES;
    
}

- (void)viewWillDisappear:(BOOL)animated {
    self.tabBarController.tabBar.hidden = NO;
}










- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
